<?php namespace Admin\Controller;

use Hdphp\Controller\Controller;

class GoodslistController extends CommonController{
	private $gamodel;
	private $glmodel;	
	public function __init(){
		parent::__init();
		//商品属性表
		$this->gamodel=new \Common\Model\Shopnature;
		//实例化货品列表模型
		$this->glmodel=new \Common\Model\Goodslist;		
	}
	
	
	
	
//	添加
	public function add(){
		
		if(!$formData=$this->glmodel->store());
//		返回异步数据	
//p($formData);
		echo json_encode($formData);
	}
	
	
	
	
//	分配数据，在页面上显示
	public function index(){
		//获得货品表数据
		$gid=Q('get.gid',0,'intval');
		$gldata=$this->glmodel->where('goods_gid='.$gid)->get();
		//分配到页面
		foreach ($gldata as $key => $value) {
			$gldata[$key]['glvalue']=explode(',', $value['glvalue']);
			foreach ($gldata[$key]['glvalue'] as $k =>$v) {
				$gldata[$key]['glvalue'][$k]=$this->gamodel->where('sid='.$v)->pluck('shopvalue');
			}
			
		} 
		View::with('gldata',$gldata);
		
    	//获得商品表数据
    	// p($gid);
		//获得商品属性表数据 及在类型属性表的信息
		$data=$this->gamodel->join('nature','nid','=','nature_nid')->where('goods_gid='.$gid)->get();
		$arr=array();
		foreach ($data as $k => $v) {
			$arr[$v['nid']][]=$v;
		}
		View::with('arr',$arr);
    	View::make();
	
	}
	
	
//	删除
	public function del(){
		//获得数据
		$glid=Q('post.glid',0,'intaval');
		//执行删除
		$this->glmodel->where('glid='.$glid)->delete();
		return json_encode($glid);
	}
	
	
	
//	修改
	public function edit(){
		//如果用户提交
		// p($_POST);
		if(IS_POST){
			if(!$this->glmodel->edit()) View::error($this->glmodel->getError());
			//成功提示
			View::success('修改成功！',U('index',array('gid'=>Q('post.goods_gid'))));
		}
		
		
		//获得旧数据
		$glid=Q('get.glid',0,'intval');
		$oldData=$this->glmodel->where('glid='.$glid)->find();
		//把属性id变为数组
		$oldData['glvalue']=explode(',', $oldData['glvalue']);
//		p($oldData);
		//分配页面
		View::with('oldData',$oldData);
		
		
//		$cmid=Q('get.cmid',0,'intval');
		$gid=$oldData['goods_gid'];
		//获得商品属性表数据 及在类型属性表的信息
		$data=$this->gamodel->join('nature','nid','=','nature_nid')->where('goods_gid='.$gid)->get();
		foreach ($data as $k => $v) {
			$arr[$v['nid']][]=$v;
		}
		View::with('arr',$arr);
	    View::make();		
	}
	

}
































 ?>